package com.gome.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gome.entitiy.Ryxx;
import com.gome.exception.GlobalException;
import com.gome.exception.GlobalExceptionDatas;
import com.gome.mapper.SecurityMapper;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;



@Service
public class SecurityService  extends ServiceImpl<SecurityMapper,Ryxx> implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if(null==username||"".equals(username)) throw  new GlobalException(GlobalExceptionDatas.ERR_CODE_NULL_LOGIN_NAME);
        Wrapper<Ryxx> wrapper=new EntityWrapper<Ryxx>();
        wrapper.eq("oc_loginname",username);
        Ryxx ryxx = super.selectOne(wrapper);
        if(null==ryxx) throw  new GlobalException(GlobalExceptionDatas.ERR_CODE_QUERY_USER);
        ryxx.setAuthorities( AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_LOGIN"));
        return ryxx;
    }
}
